import org.jh.Account
import org.jh.TransferService

scenario "transfer fund, normal case", {
	given "Account-A and Account-B",{
		account1 = new Account("A", 100)
		account2 = new Account("B", 50)
	}	
	when "transfer 100 from ${account1} to ${account2}", {
		new TransferService().transactionalTransfer account1, account2, 100
	}
	then "the final balances are ${account1} and ${account2}", {
		account1.balance.shouldBe 0
		account2.balance.shouldBe 150
	}
}

scenario "insufficient fund transfer", {
	given "Account-A and Account-B",{
		account1 = new Account("A", 100)
		account2 = new Account("B",50)
	}	
	when "transfer 150 from ${account1} to ${account2}", {
		ensureThrows(RuntimeException) {
			new TransferService().transactionalTransfer account1, account2, 150
		}
	}
	then "the ${account1} and ${account2} are not changed", {
		account1.balance.shouldBe 100
		account2.balance.shouldBe 50
	}
}

scenario "non transactional insufficient fund transfer", {
	given "Account-A and Account-B",{
		account1 = new Account("A", 100)
		account2 = new Account("B", 50)
	}	
	when "transfer 150 non-transactionally from ${account1} to ${account2}", {
		ensureThrows(RuntimeException) {
			new TransferService().nonTransactionalTransfer account1, account2, 150
		}
	}
	then "balance for ${account2} is credited but ${account1} is not debited!!", {
		account1.balance.shouldBe 100
		account2.balance.shouldBe 200
	}
}
